"""243"""
from typing import List

"""
请设计一个类，使该类的构造函数能够接收一个字符串数组。然后再实现一个方法，该方法能够分别接收两个单词，并返回列表中这两个单词之间的最短距离。

实现 WordDistanc 类:
    WordDistance(String[] wordsDict) 用字符串数组 wordsDict 初始化对象。
    int shortest(String word1, String word2) 返回数组 worddict 中 word1 和 word2 之间的最短距离。
"""
"""
示例 1:

输入: 
    ["WordDistance", "shortest", "shortest"]
    [[["practice", "makes", "perfect", "coding", "makes"]], ["coding", "practice"], ["makes", "coding"]]
输出:
    [null, 3, 1]

解释：
    WordDistance wordDistance = new WordDistance(["practice", "makes", "perfect", "coding", "makes"]);
    wordDistance.shortest("coding", "practice"); // 返回 3
    wordDistance.shortest("makes", "coding");    // 返回 1
 

注意:
    1 <= wordsDict.length <= 3 * 104
    1 <= wordsDict[i].length <= 10
    wordsDict[i] 由小写英文字母组成
    word1 和 word2 在数组 wordsDict 中
    word1 != word2
    shortest 操作次数不大于 5000 
"""


class WordDistance:

    def __init__(self, wordsDict: List[str]):
        self.load = dict()
        self.cnt = 0
        for s in wordsDict:
            if s not in self.load:
                self.load[s] = [self.cnt]
            else:
                self.load[s].append(self.cnt)
            self.cnt += 1

    def shortest(self, word1: str, word2: str) -> int:
        ans = float("Inf")
        if word1 == word2:
            return 0
        tmp1, tmp2 = self.load[word1], self.load[word2]
        for v in tmp1:
            for vv in tmp2:
                ans = min(ans, abs(v - vv))
        return ans


if __name__ == '__main__':
    w = WordDistance(["practice", "makes", "perfect", "coding", "makes"])
    w.shortest("coding", "practice")
    w.shortest("makes", "coding")
